<?php

namespace common\models\searchs;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\DhUser;

/**
 * SearchUser represents the model behind the search form about `\common\models\DhUser`.
 */
class SearchUser extends DhUser
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'sex', 'flag', 'create_time'], 'integer'],
            [['nickname', 'header', 'tel', 'session_code'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = DhUser::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        if (!($this->load($params) && $this->validate())) {
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id' => $this->id,
            'sex' => $this->sex,
            'flag' => $this->flag,
            'create_time' => $this->create_time,
        ]);

        $query->andFilterWhere(['like', 'nickname', $this->nickname])
            ->andFilterWhere(['like', 'header', $this->header])
            ->andFilterWhere(['like', 'tel', $this->tel])
            ->andFilterWhere(['like', 'session_code', $this->session_code]);

        return $dataProvider;
    }
}
